

<!doctype html>

<html lang="en" data-content_root="../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>skidl.package &#8212; SKiDL 1.2.2 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=fa44fd50" />
    <link rel="stylesheet" type="text/css" href="../../_static/bizstyle.css?v=a66e137f" />
    
    <script src="../../_static/documentation_options.js?v=fd7cadf9"></script>
    <script src="../../_static/doctools.js?v=9a2dae69"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../_static/bizstyle.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <!--[if lt IE 9]>
    <script src="_static/css3-mediaqueries.js"></script>
    <![endif]-->
  </head><body>
    <h1 id="site-title"><a href="../../../../"><img src="../../../../images/slim_banner.png" width="100%"></a></h1>
    
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SKiDL 1.2.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">skidl.package</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for skidl.package</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>

<span class="c1"># The MIT License (MIT) - Copyright (c) Dave Vandenbout.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Package a subcircuit so it can be used like a Part.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="p">(</span>  <span class="c1"># isort:skip</span>
    <span class="n">absolute_import</span><span class="p">,</span>
    <span class="n">division</span><span class="p">,</span>
    <span class="n">print_function</span><span class="p">,</span>
    <span class="n">unicode_literals</span><span class="p">,</span>
<span class="p">)</span>

<span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">super</span><span class="p">,</span> <span class="nb">zip</span>
<span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">copy</span>

<span class="kn">from</span> <span class="nn">deprecation</span> <span class="kn">import</span> <span class="n">deprecated</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
    <span class="n">standard_library</span><span class="o">.</span><span class="n">install_aliases</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>

<span class="kn">from</span> <span class="nn">.bus</span> <span class="kn">import</span> <span class="n">Bus</span>
<span class="kn">from</span> <span class="nn">.group</span> <span class="kn">import</span> <span class="n">subcircuit</span>
<span class="kn">from</span> <span class="nn">.interface</span> <span class="kn">import</span> <span class="n">Interface</span>
<span class="kn">from</span> <span class="nn">.net</span> <span class="kn">import</span> <span class="n">Net</span>
<span class="kn">from</span> <span class="nn">.part</span> <span class="kn">import</span> <span class="n">NETLIST</span>
<span class="kn">from</span> <span class="nn">.protonet</span> <span class="kn">import</span> <span class="n">ProtoNet</span>
<span class="kn">from</span> <span class="nn">.utilities</span> <span class="kn">import</span> <span class="n">export_to_all</span>



<div class="viewcode-block" id="Package">
<a class="viewcode-back" href="../../rst_output/skidl.package.html#skidl.package.Package">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">class</span> <span class="nc">Package</span><span class="p">(</span><span class="n">Interface</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="p">[</span><span class="s2">&quot;circuit&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Don&#39;t use update(). It doesn&#39;t seem to call __setitem__.</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="bp">self</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>  <span class="c1"># Use __setitem__ so both dict item and attribute are created.</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># def __call__(self, *args, **kwargs):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Create a copy of a package.&quot;&quot;&quot;</span>

        <span class="c1"># Get circuit that will contain the package subcircuitry.</span>
        <span class="n">circuit</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;circuit&quot;</span><span class="p">,</span> <span class="n">default_circuit</span><span class="p">)</span>

        <span class="c1"># See if this package should be instantiated into the netlist or used as a template.</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;dest&quot;</span><span class="p">,</span> <span class="n">NETLIST</span><span class="p">)</span>

        <span class="c1"># Create a blank Package object.</span>
        <span class="n">pckg</span> <span class="o">=</span> <span class="n">Package</span><span class="p">()</span>

        <span class="c1"># Add I/O and anything else to the blank Package.</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">ProtoNet</span><span class="p">):</span>
                <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">circuit</span> <span class="o">=</span> <span class="n">circuit</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="n">Bus</span><span class="p">)):</span>
                <span class="k">if</span> <span class="n">v</span><span class="o">.</span><span class="n">is_implicit</span><span class="p">():</span>
                    <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">circuit</span><span class="o">=</span><span class="n">circuit</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Should this use copy()?</span>
                    <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
                    <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">circuit</span> <span class="o">=</span> <span class="n">circuit</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>

        <span class="c1"># Add passed-in attributes to the package.</span>
        <span class="c1"># Don&#39;t use update(). It doesn&#39;t seem to call __setitem__.</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="n">pckg</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>  <span class="c1"># Use __setitem__ so both dict item and attribute are created.</span>

        <span class="c1"># Assign subcircuit creation function.</span>
        <span class="n">pckg</span><span class="o">.</span><span class="n">subcircuit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">subcircuit</span>

        <span class="c1"># Remove creation function so it&#39;s not passed as a parameter.</span>
        <span class="k">del</span> <span class="n">pckg</span><span class="p">[</span><span class="s2">&quot;subcircuit&quot;</span><span class="p">]</span>

        <span class="c1"># Add package to circuit only if it&#39;s supposed to be instantiated.</span>
        <span class="k">if</span> <span class="n">dest</span> <span class="o">==</span> <span class="n">NETLIST</span><span class="p">:</span>
            <span class="n">circuit</span> <span class="o">+=</span> <span class="n">pckg</span>

        <span class="k">return</span> <span class="n">pckg</span>

<div class="viewcode-block" id="Package.is_movable">
<a class="viewcode-back" href="../../rst_output/skidl.package.html#skidl.package.Package.is_movable">[docs]</a>
    <span class="k">def</span> <span class="nf">is_movable</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">obj</span><span class="o">.</span><span class="n">is_movable</span><span class="p">():</span>
                    <span class="k">return</span> <span class="kc">False</span>  <span class="c1"># Interface is not movable if any object in it is not movable.</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">pass</span>  <span class="c1"># Objects without is_movable() are always movable.</span>
        <span class="k">return</span> <span class="kc">True</span>  <span class="c1"># Every object in the Interface that could move was movable.</span></div>
</div>



<div class="viewcode-block" id="package">
<a class="viewcode-back" href="../../rst_output/skidl.package.html#skidl.package.package">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="nd">@deprecated</span><span class="p">(</span><span class="n">deprecated_in</span><span class="o">=</span><span class="s2">&quot;1.2.0&quot;</span><span class="p">,</span> <span class="n">removed_in</span><span class="o">=</span><span class="s2">&quot;2.0.0&quot;</span><span class="p">,</span> <span class="n">details</span><span class="o">=</span><span class="s2">&quot;Use Interface instead.&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">package</span><span class="p">(</span><span class="n">subcirc_func</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Decorator that creates a package for a subcircuit routine.&quot;&quot;&quot;</span>

    <span class="n">pckg</span> <span class="o">=</span> <span class="n">Package</span><span class="p">()</span>  <span class="c1"># Create the package.</span>

    <span class="c1"># Store the parameter names passed to the subcircuit.</span>
    <span class="n">code</span> <span class="o">=</span> <span class="n">subcirc_func</span><span class="o">.</span><span class="vm">__code__</span>
    <span class="n">num_args</span> <span class="o">=</span> <span class="n">code</span><span class="o">.</span><span class="n">co_argcount</span>
    <span class="n">arg_names</span> <span class="o">=</span> <span class="n">code</span><span class="o">.</span><span class="n">co_varnames</span><span class="p">[:</span><span class="n">num_args</span><span class="p">]</span>

    <span class="c1"># By default, set parameters to a package to be ProtoNets.</span>
    <span class="k">for</span> <span class="n">arg_name</span> <span class="ow">in</span> <span class="n">arg_names</span><span class="p">:</span>
        <span class="n">pn</span> <span class="o">=</span> <span class="n">ProtoNet</span><span class="p">(</span><span class="n">arg_name</span><span class="p">,</span> <span class="n">circuit</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
        <span class="n">pn</span><span class="o">.</span><span class="n">intfc</span> <span class="o">=</span> <span class="n">pckg</span>
        <span class="n">pn</span><span class="o">.</span><span class="n">intfc_key</span> <span class="o">=</span> <span class="n">arg_name</span>
        <span class="n">pckg</span><span class="p">[</span><span class="n">arg_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">pn</span>

    <span class="c1"># Set any default values for the parameters.</span>
    <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">subcirc_func</span><span class="p">,</span> <span class="s2">&quot;__defaults__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">dflt_value</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span>
            <span class="nb">reversed</span><span class="p">(</span><span class="n">arg_names</span><span class="p">),</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">subcirc_func</span><span class="o">.</span><span class="vm">__defaults__</span><span class="p">)</span>
        <span class="p">):</span>
            <span class="n">pckg</span><span class="p">[</span><span class="n">arg_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">dflt_value</span>
    <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">subcirc_func</span><span class="p">,</span> <span class="s2">&quot;__kwdefaults__&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">arg_name</span><span class="p">,</span> <span class="n">dflt_value</span> <span class="ow">in</span> <span class="n">subcirc_func</span><span class="o">.</span><span class="vm">__kwdefaults__</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">pckg</span><span class="p">[</span><span class="n">arg_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">dflt_value</span>

    <span class="c1"># Create the subcircuit function that will be called to insantiate this package.</span>
    <span class="n">pckg</span><span class="o">.</span><span class="n">subcircuit</span> <span class="o">=</span> <span class="n">subcircuit</span><span class="p">(</span><span class="n">subcirc_func</span><span class="p">)</span>

    <span class="c1"># Remove the subcircuit key from the dict so it won&#39;t be passed to subcirc_func().</span>
    <span class="k">del</span> <span class="n">pckg</span><span class="p">[</span><span class="s2">&quot;subcircuit&quot;</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">pckg</span></div>

</pre></div>

            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SKiDL 1.2.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">skidl.package</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
    &#169; Copyright 2016-2024, Dave Vandenbout.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.3.7.
    </div>
  </body>
</html>